﻿
#include "web_server.h"

Web_Server::Web_Server()
{
}

Web_Server::~Web_Server()
{
    qDebug() << "Web_Server析构";
    delete m_protocol;
}

int Web_Server::objectInit(std::string mode, void* Para_struct)
{
    if (QString::fromStdString(mode) == "TCP") {
        this->m_protocol = new TcpClient();
        m_protocol->protocolParaInit(Para_struct);
        m_protocol->setProtocolReadCallbackFunc(SnCodeRecCallback);
        m_protocol->protocolOpen();

    } else {
        qDebug() << "invalid mode";
        return -1;
    }

    return 0;
}

int Web_Server::sendDeviceData(void* device_struct)
{
    if (m_sn_code.isEmpty()) { // 没有接收到SN号
        qDebug() << "没有接收到SN号";
        return -1;
    }

    this->m_webServerDeviceData = (WebServerDeviceData_TypeDef*)device_struct;
    QString device_data;

    QDateTime time = QDateTime::currentDateTime();
    QString hour = QString("%1").arg(time.time().hour(), 2, 10, QLatin1Char('0'));
    QString minute = QString("%1").arg(time.time().minute(), 2, 10, QLatin1Char('0'));
    QString second = QString("%1").arg(time.time().second(), 2, 10, QLatin1Char('0'));
    device_data = "$" + m_sn_code + "DE01" + QString::fromStdString(m_webServerDeviceData->device_code) + '|' + "1|"
        + QString::fromStdString(m_webServerDeviceData->enterprise_identification_code) + '|' + QString::fromStdString(m_webServerDeviceData->province_name) + '|'
        + QString::fromStdString(m_webServerDeviceData->device_model) + '|' + QString::fromStdString(m_webServerDeviceData->device_name) + "|300|||"
        + hour + minute + second + "|v2.0.0$";

    return m_protocol->protocolWrite(device_data.toUtf8());
}

int Web_Server::sendCharacterData(void* character_struct)
{
    if (m_sn_code.isEmpty()) { // 没有接收到SN号
        qDebug() << "没有接收到SN号";
        return -1;
    }

    WebServerCharacterData_TypeDef* m_webServerCharacterData = (WebServerCharacterData_TypeDef*)character_struct;

    if (((m_webServerCharacterData->time_stamp / 1000000000) < 1) || ((m_webServerCharacterData->time_stamp / 1000000000) > 4)) {
        qDebug() << "时间戳错误";
        return -2;
    }

    QDateTime dateTime = QDateTime::fromSecsSinceEpoch(m_webServerCharacterData->time_stamp);
    QString hour = QString("%1").arg(dateTime.time().hour(), 2, 10, QLatin1Char('0'));
    QString minute = QString("%1").arg(dateTime.time().minute(), 2, 10, QLatin1Char('0'));
    QString second = QString("%1").arg(dateTime.time().second(), 2, 10, QLatin1Char('0'));

    QString character_data;
    character_data = "$" + m_sn_code + "FE01" + QString::fromStdString(m_webServerDeviceData->device_code) + '|' + QString::fromStdString(m_webServerCharacterData->data_type_label) + '|'
        + QString::fromStdString(m_webServerCharacterData->channel_label) + '|' + QString::fromStdString(m_webServerCharacterData->character_data_str)
        + '|' + hour + minute + second + '$';
    ;

    return m_protocol->protocolWrite(character_data.toUtf8());
}

void SnCodeRecCallback(const char data[])
{
    qDebug() << "tcp回调函数调用";
    m_sn_code = QString::fromLocal8Bit(data, 4);
    qDebug() << m_sn_code;
}
